package com.bole.controller;


import com.bole.data.Result;
import com.bole.data.ResultEnum;
import com.bole.dto.user.DicCarSpecDto;
import com.bole.feign.CarSpecFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("spec")
@Api(value="规格接口")
public class CarSpecController {

    @Autowired
    private CarSpecFeign carSpecFeign;


    @GetMapping("tolist")
    @ApiOperation(value = "跳转规格页面")
    public String tolist(){
        return "car-spec";
    }

    @PostMapping("getSpecInfo")
    @ApiOperation(value = "查询规格信息")
    @ResponseBody
    public Result getSpecInfo(){
        List<DicCarSpecDto> list =  carSpecFeign.getSpecInfo();
        return Result.response(ResultEnum.OK).setData(list);
    }


    @PostMapping("saveSpecInfo")
    @ApiOperation(value = "增加规格")
    @ApiImplicitParam(name = "dicCarSpecDto",value = "规格",required = true,dataTypeClass = DicCarSpecDto.class)
    @ResponseBody
    public Result saveSpecInfo(@RequestBody DicCarSpecDto dicCarSpecDto){
        carSpecFeign.saveSpecInfo(dicCarSpecDto);
        return Result.response(ResultEnum.OK);
    }


    @PostMapping("toUpdate/{id}")
    @ApiOperation(value = "回显")
    @ApiImplicitParam(name = "id",value = "规格id",required = true,dataTypeClass = Long.class)
    @ResponseBody
    public Result toUpdate(@PathVariable("id")Long id){
        DicCarSpecDto dicCarSpecDto = carSpecFeign.toUpdate(id);
        return Result.response(ResultEnum.OK).setData(dicCarSpecDto);
    }


    @PostMapping("updateSpecInfo")
    @ApiOperation(value = "修改规格")
    @ApiImplicitParam(name = "dicCarSpecDto",value = "规格",required = true,dataTypeClass = DicCarSpecDto.class)
    @ResponseBody
    public Result updateSpecInfo(@RequestBody DicCarSpecDto dicCarSpecDto){
        carSpecFeign.updateSpecInfo(dicCarSpecDto);
        return Result.response(ResultEnum.OK);
    }


    @PostMapping("deleteBatch/{ids}")
    @ApiOperation(value = "批量删除")
    @ApiImplicitParam(name = "ids",value = "规格id",required = true,paramType = "path",dataTypeClass = Long.class)
    @ResponseBody
    public Result deleteBatch(@PathVariable("ids")Long[] ids){
        List<Long> ides = new ArrayList<>();
        for (Long id : ids) {
            ides.add(id);
        }
        carSpecFeign.deleteBatch(ides);
        return Result.response(ResultEnum.OK);
    }






}
